Bscotch Utilities
Utility and helper methods and types for common programming problems in Node.js.
Installation
Requirements
Installation
From npm:
npm install @bscotch/utility
Features
The following listings show a subset of the utilities found in this package.
Strings
import {
capitalize,
decodeFromBase64,
encodeToBase64,
decodeFromBase64JsonString,
encodeToBase64JsonString,
explode,
nodent,
oneline,
undent,
} from '@bscotch/utility';
Paths
import { toPosixPath, sortedPaths, parentPaths } from '@bscotch/utility';
parentPaths('/hello/world/foo/bar.txt');
Files
import {
listPathsSync,
listFoldersSync,
listFilesSync,
listFilesByExtensionSync,
removeEmptyDirsSync,
} from '@bscotch/utility';
const recursive = true;
listPathsSync('.', recursive);
listFoldersSync('.', recursive);
listFilesSync('.', recursive);
listFilesByExtensionSync('.', 'txt', recursive);
listFilesByExtensionSync('.', ['txt', 'md'], recursive);
removeEmptyDirsSync('.');
Waits
import {
waitForMillis,
waitForSeconds,
waitForTick,
} from '@bscotch/utility';
async myAsynFunction(){
await waitForMillis(1000);
await waitForSeconds(1);
await waitForTick();
}
Objects
import {
isPlainObject,
isPlainObjectOrArray,
asObjectIfArray,
flattenObjectPaths,
objectPaths,
getValueAtPath,
setValueAtPath,
objectPathsFromWildcardPath,
transformValueByPath,
} from '@bscotch/utility';
asObjectIfArray(['hello']);
const testObject = {
hello: 'world',
nested: {
layer: 1,
array: [4, 6, 7],
},
};
flattenObjectPaths(testObject);
objectPaths(testObject);
getValueAtPath(testObject, 'nested.array.2');
setValueAtPath(testObject, 'new.0.field', 10);
objectPathsFromWildcardPath('nested.*', testObject);
objectPathsFromWildcardPath('nested.array.*', testObject);
transformValueByPath(testObject, 'nested.array.*', (n) => ++n);
Crypto
import {
md5,
sha1,
sha256,
createHash,
encrypt,
decrypt,
} from '@bscotch/utility';
let hash = md5('hello world');
hash = sha256('hello world', 'base64');
hash = createHash('sha1', 'hello world');
const key = '00000000000000000000000000000000';
const encrypted = encrypt('Hello World', key);
const sourceAsBuffer = decrypt(encrypted, key);
Dates
import {
dateSort,
dateSortDescending,
dateDifferenceMillis,
dateDifferenceSeconds,
dateDifferenceMinutes,
dateDifferenceHours,
dateDifferenceDays,
dateIsGreaterThan,
dateIsInTheFuture,
dateIsInThePast,
dateIsLessThan,
dateIsOlderThanMillisAgo,
dateIsOlderThanSecondsAgo,
dateIsOlderThanMinutesAgo,
dateIsOlderThanHoursAgo,
dateIsOlderThanDaysAgo,
isDate,
isValidDate,
assertIsValidDate
} from '@bscotch/utility';
Arrays
import {
arrayWrapped,
arrayUnwrapped,
arrayIsIncreasing,
arraySortNumeric,
arraySortNumericDescending,
} from '@bscotch/utility';
arrayIsIncreasing([-10, 99, 1111]);
arrayWrapped('hello');
arrayWrapped(['hello']);
arrayWrapped(undefined);
arrayUnwrapped('hello');
arrayUnwrapped(['hello']);
arrayUnwrapped(['hello', 'goodbye']);